package com.example.teaculture.activity;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.webkit.WebBackForwardList;
import android.webkit.WebChromeClient;
import android.webkit.WebResourceRequest;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.teaculture.DB.FavorDao;
import com.example.teaculture.DB.HistoryDao;
import com.example.teaculture.R;
import com.example.teaculture.application.MyApplication;
import com.example.teaculture.model.Favor;
import com.yechaoa.yutils.ToastUtil;

import java.util.Objects;

public class FavorDetailActivity extends AppCompatActivity {

    private WebView webView;
    private TextView tv_title;
    private ImageView iv_back;
    private ImageView iv_share;
    private ImageView iv_favor;
    FavorDao favorDao = new FavorDao(this);
    private ImageView iv_favor_two;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_favor_detail);

        webView = findViewById(R.id.web_favor);
        tv_title = findViewById(R.id.tv_title);
        iv_back = findViewById(R.id.iv_back);
        iv_favor = findViewById(R.id.iv_favor);
        iv_share = findViewById(R.id.iv_share);
        iv_favor_two = findViewById(R.id.iv_favor_two);
        WebSettings settings = webView.getSettings();
        settings.setJavaScriptEnabled(true);


        //加载数据
        Intent intent = getIntent();
        String url = intent.getStringExtra("url");
        String title = intent.getStringExtra("title");
        tv_title.setText(title);

        Favor select = favorDao.selectByTitle(title);
        if(Objects.isNull(select)){
            iv_favor_two.setVisibility(View.GONE);
            iv_favor.setVisibility(View.VISIBLE);
        }else{
            iv_favor_two.setVisibility(View.VISIBLE);
            iv_favor.setVisibility(View.GONE);
        }

        //支持缩放
        settings.setUseWideViewPort(true);//设定支持viewport
        settings.setLoadWithOverviewMode(true);
        settings.setBuiltInZoomControls(true);
        settings.setSupportZoom(true);//设定支持缩放



        iv_favor.setOnClickListener(v->{
            Favor favor = new Favor();
            favor.setTitle(title);
            favor.setUrl(url);
            favorDao.insertDetsilNews(favor);
            iv_favor_two.setVisibility(View.VISIBLE);
            iv_favor.setVisibility(View.GONE);
            ToastUtil.show("收藏成功");
        });

        iv_favor_two.setOnClickListener(v->{
            favorDao.delByTitle(title);
            iv_favor_two.setVisibility(View.GONE);
            iv_favor.setVisibility(View.VISIBLE);
            ToastUtil.show("取消收藏成功");
        });

        iv_share.setOnClickListener(v->{
            Favor favor = MyApplication.getInstance().favor;
            Intent intent_share = new Intent(Intent.ACTION_SEND);
            intent_share.setType("text/plain");
            intent_share.putExtra(Intent.EXTRA_SUBJECT, "分享");
            intent_share.putExtra(Intent.EXTRA_TEXT, "\"" + favor.getTitle() + "\", " + favor.getUrl());
            intent_share.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            startActivity(Intent.createChooser(intent_share, getTitle()));
        });
        // 设置与Js交互的权限
        settings.setJavaScriptEnabled(true);
        webView.setWebChromeClient(new WebChromeClient() {
            @Override
            public void onReceivedTitle(WebView view, String title) {
                super.onReceivedTitle(view, title);
                Log.i("title", "onReceivedTitle: " + title);
                Favor history = new Favor();
                HistoryDao function = new HistoryDao(FavorDetailActivity.this);
                WebBackForwardList webBackForwardList = webView.copyBackForwardList();
                int position = webBackForwardList.getCurrentIndex();
                history.setTitle(title);
                history.setUrl(webBackForwardList.getItemAtIndex(position).getUrl());
                function.insertDetsilNews(history);
            }
        });

        webView.setWebViewClient(new WebViewClient() {
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
                return super.shouldOverrideUrlLoading(view, request);
            }
        });


        //打开的网址
        webView.loadUrl(url);
        webView.setOnKeyListener((v, keyCode, event) -> {
            if ((keyCode == KeyEvent.KEYCODE_BACK) && webView.canGoBack()) {
                webView.goBack();
                return true;
            }
            return false;
        });

        iv_back.setOnClickListener(v -> {
            this.finish();
        });
    }
}